<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2018/6/21
 * Time: 16:50
 */

namespace app\common\model;

use think\Cache;

/**
 * 准了商品模型相关接口
 * Class ZhunleGoods
 * @package app\common\model
 */
class ZhunleGoods extends Base
{
    protected $table = 'wechat_backup_07_18_14_43_00.zhunle_goods';

    const ALL_GOODS = 'all:goods'; //所有的商品缓存键名
    const GOODS_INFO = 'goods:info:'; //商品信息缓存键名
    const CACHE_TIME = 86400 * 7; //缓存时间

    /**
     * 获取所有的商品信息
     * @return array
     */
    public function getAllGoods()
    {
        $gids = false;

        $data_list = [];

        if ($this->cache_on >= 1) {
            $gids = Cache::get(self::ALL_GOODS);
        }

        if ($gids === false) {
            try {
                $data = $this->where('status', 1)->column('id');

                if ($gids) {
                    $gids = implode(',', $data);
                }

                if ($gids && $this->cache_on >= 1) Cache::set(self::ALL_GOODS, $gids, self::CACHE_TIME);

            } catch (\Exception $e) {
                $this->error = '获取商品失败';
                return false;
            }
        }

        $gids = explode(',', $gids);

        if (!empty($gids)) {
            $ac_ids = '';
            $temp = [];

            foreach ($gids as $ac_id) {
                if ($this->cache_on >= 1) {
                    // 根据商品id 获取商品内容
                    $temp[$ac_id] = Cache::get(self::GOODS_INFO . $ac_id);
                } else {
                    // 缓存未开启时，默认从数据库获取
                    $temp[$ac_id] = false;
                }
                // 商品内容未缓存，等待从数据库中获取
                if ($temp[$ac_id] === false && $ac_id) {
                    $ac_ids .= $ac_id . ','; //未缓存的商品id
                }
            }

            if ($ac_ids !== '' || strlen($ac_ids) > 0) {
                //去掉结尾的 ，
                $ac_ids = substr($ac_ids, 0, strlen($ac_ids) - 1);

                try {
                    $where = ['status' => 1];
                    $data = $this
                        ->field('id,name.description')
                        ->where($where)
                        ->where('id in (' . $ac_ids . ')')
                        ->select();
                    $data = json_decode(json_encode($data, true), true);
                } catch (\Exception $e) {
                    $this->error = '获取商品信息失败';
                    return false;
                }

                if (!empty($data)) {
                    foreach ($data as $c) {
                        // 将商品内容缓存
                        if ($this->cache_on >= 1) {
                            Cache::set(self::GOODS_INFO . $c['id'], $c, self::CACHE_TIME);
                        }
                        // 将数据库查出来的内容 写入到数组中为空的部分
                        if ($c) {
                            $temp[$c['id']] = $c;
                        }
                    }
                }

                if (!empty($temp)) {
                    foreach ($temp as $item) {
                        if ($item) {
                            $data_list[] = $item;
                        }
                    }
                }

            }
        }

        return $data_list;
    }

    /**
     * 获取具体的商品信息
     * @param array $map
     * @return array|bool|mixed|\PDOStatement|string|\think\Model
     */
    public function getGoodsInfo($map = [])
    {
        $gid = $map['gid'];

        $data = false;

        if ($this->cache_on >= 1) {
            $data = Cache::get(self::GOODS_INFO . $gid);
        }

        if ($data === false) {
            $where = ['id' => $gid, 'status' => 1];

            try {
                $data = $this
                    ->where($where)
                    ->findOrFail();
                $data = json_decode(json_encode($data, true), true);
                if ($this->cache_on >= 1) {
                    Cache::set(self::GOODS_INFO . $gid, $data, self::CACHE_TIME);
                }
            } catch (\Exception $e) {
                $this->error = '获取商品信息失败';
                return false;
            }
        }

        return $data;
    }
}
